/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FhCompList;
import me.zhengjie.domain.TestModel;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.FhCompListRepository;
import me.zhengjie.service.FhCompListService;
import me.zhengjie.service.dto.FhCompListDto;
import me.zhengjie.service.dto.FhCompListQueryCriteria;
import me.zhengjie.service.mapstruct.FhCompListMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import java.text.DateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author admin
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class FhCompListServiceImpl implements FhCompListService {

    private final FhCompListRepository fhCompListRepository;
    private final FhCompListMapper fhCompListMapper;

    @Override
    public Map<String,Object> queryAll(FhCompListQueryCriteria criteria, Pageable pageable){
        Page<FhCompList> page = fhCompListRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fhCompListMapper::toDto));
    }

    @Override
    public List<FhCompListDto> queryAll(FhCompListQueryCriteria criteria){
        return fhCompListMapper.toDto(fhCompListRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FhCompListDto findById(Integer id) {
        FhCompList fhCompList = fhCompListRepository.findById(id).orElseGet(FhCompList::new);
        ValidationUtil.isNull(fhCompList.getId(),"FhCompList","id",id);
        return fhCompListMapper.toDto(fhCompList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FhCompListDto create(FhCompList resources) {
        return fhCompListMapper.toDto(fhCompListRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FhCompListDto> createAll(ArrayList<FhCompList> resources) {
       List<FhCompList> fhCompLists = fhCompListRepository.saveAll(resources);
       return fhCompLists.stream().map(fhCompListMapper::toDto).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FhCompList resources) {
        FhCompList fhCompList = fhCompListRepository.findById(resources.getId()).orElseGet(FhCompList::new);
        ValidationUtil.isNull( fhCompList.getId(),"FhCompList","id",resources.getId());
        fhCompList.copy(resources);
        fhCompListRepository.save(fhCompList);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fhCompListRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FhCompListDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
//        List<String> scaleList = new ArrayList<>({"1-20人","20-100人","100-500人","500人以上"})
//        String [] scaleList = {"1-20人","20-100人","100-500人","500人以上"};
        for (FhCompListDto fhCompList : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("企业名称", fhCompList.getName());
            map.put("法人代表", fhCompList.getLegalPerson());
            map.put("注册资本", fhCompList.getCapital()+"万元");
            map.put("执照开始时间", DateFormat.getDateInstance().format(fhCompList.getLicenseStartTime()));
            map.put("执照结束时间", DateFormat.getDateInstance().format(fhCompList.getLicenseEndTime()));
            map.put("规模", fhCompList.getScale());
            map.put("企业收入", fhCompList.getIncome()+"万元");
            map.put("企业税收", fhCompList.getRevenue()+"万元");
            map.put("所属行业", fhCompList.getIndustry().getName());
            map.put("入驻时间  ", DateFormat.getDateInstance().format(fhCompList.getEnterTime()));
            map.put("是否重点", fhCompList.getEmphasis());
            map.put("是否规上", fhCompList.getSupra());
            map.put("所在楼", fhCompList.getInfoFloor().getName());
            map.put("公司图片", fhCompList.getImgUrl());
            map.put("所在层", fhCompList.getLevel());
            map.put("门牌号", fhCompList.getDoorplate());
            map.put("公司简介", fhCompList.getIntroduction());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void upload(MultipartFile file) throws IOException {
        List<Object> list = EasyExcelUtil.readExcel(file, new TestModel(),1,1);
        if(list != null && list.size() > 0){
            for(Object o : list){
                TestModel testModel = (TestModel) o;
                System.out.println(testModel.toString());
            }
        }
    }

    @Override
    public List countAllGroupBySupra() {
        return  fhCompListRepository.totalBySupra();
    }
}